import operate from '../common/operate.js'
// import store from '../store/index.js'  
export default class Request {
	http(param) {
		// 请求参数
		var url = param.url,
			method = param.method || 'get',
			header = {},
			data = param.data || {},
			token = param.token || "",
			hideLoading = param.hideLoading || false,
			isJson = param.isJson || false,
			isShowmsg = param.isShowmsg || false
		//拼接完整请求地址
		var requestUrl = operate.api + url;
		let Authorization = uni.getStorageSync('logintoken') || '';
		let currency = uni.getStorageSync('currency') || 'CNY';
		let lang = ''; //语言

		if (uni.getStorageSync('lang').lang == 'zh') {
			lang = 'zh-cn'
		} else if (uni.getStorageSync('lang').lang == 'hk') {
			lang = 'zh-hk'
		} else {
			lang = 'en-us'
		}
		//请求方式:GET或POST(POST需配置
		let locInfo = uni.getStorageSync('locInfo');
		if (locInfo) {
			locInfo = JSON.parse(locInfo)
			header = {
				'lat': locInfo.latitude,
				'lon': locInfo.longitude,
			}
		}
		if (method) {
			method = method.toUpperCase(); //小写改为大写
			header['Accept'] = 'application/json'
			if (method == "POST" && !isJson) {
				header['content-type'] = "application/x-www-form-urlencoded"
			} else {
				header['content-type'] = "application/json"
			}
			if (Authorization) {
				header['token'] = Authorization
			}
			if (currency) {
				header['currency'] = currency
			}
			if (lang) {
				header['lang'] = lang
			}
			// console.log(header)
		}

		//加载圈
		if (!hideLoading) {
			uni.showLoading({
				title: '加载中...'
			});
		}

		// 返回promise
		return new Promise((resolve, reject) => {
			// 请求
			uni.request({
				url: requestUrl,
				data: data,
				method: method,
				header: header,
				success: (res) => {
					// console.log(res)
					// 判断 请求api 格式是否正确
					if (res.statusCode && res.statusCode != 200) {
						// console.log(res.data)
						// uni.showToast({
						//     title: "api错误" + res.errMsg,
						//     icon: 'none'
						// });
						if (res.data.code == 401) {
							// uni.showToast({
							// 	title: res.data.msg,
							// 	icon: 'none'
							// });
							uni.removeStorageSync('logintoken');
							uni.removeStorageSync('bingCardInfo');
							let hash = window.location.hash=='#/pages/login/login'?'':window.location.hash
							console.log(hash)
							uni.setStorageSync('hash',hash)
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/login/login'
								})
							}, 500)
						}
						return;
					}
					// code判断:200成功,不等于200错误
					if (res.data.code >= 1) {

						if (res.data.code != 1) {
							setTimeout(() => {
								uni.showToast({
									title: res.data.msg,
									icon: 'none',
								});
							}, 30)
							// console.log(JSON.stringify(res))
							if (res.data.code == 400) {
								// setTimeout(()=>{
								// 	uni.navigateTo({
								// 		url:'/pages/business/join'
								// 	})
								// },1000)
							}

							if (res.data.code == 401) {
								uni.removeStorageSync('logintoken');
								uni.removeStorageSync('bingCardInfo');
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/login/login'
									})
								}, 500)
							}
							return;
						}
					} else {
						if (isShowmsg) {
							if (res.data.code == -2) {

							} else if (res.data.code == -3) {

							} else {
								uni.showToast({
									title: res.data.msg,
									icon: 'none'
								});
							}
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
						}
						if(res.data.code == -4){
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/login/Nopermission'
								})
							}, 300)
						}
						// return;
					}
					// 将结果抛出
					resolve(res.data)
				},
				//请求失败
				fail: (e) => {
					uni.showToast({
						title: "" + e.errMsg,
						icon: 'none'
					});
					resolve(e.data);
				},
				//请求完成
				complete() {
					//隐藏加载
					if (!hideLoading) {
						uni.hideLoading();
					}
					resolve();
					return;
				}
			})
		})
	}
	//上传类
	upLoadFile(options,pixel) {
		let formDataWithCompression
		if (pixel) {
			console.log(pixel,'2');
			formDataWithCompression = {
			            ...options.formData,
			            'x:qn-process': 'imageMogr2/thumbnail/!'+pixel+'x>'
			};
		} else{
			
		}
		
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: operate.ossapi,
				filePath: options.filePath,
				name: options.name,
				formData: pixel?formDataWithCompression:options.formData,
				header: options.header || {},
				success: (res) => {
					console.log(res)
					if (res.statusCode == 200) {
						let data = JSON.parse(res.data).key;
						resolve(data)
					} else {
						resolve(res)
					}
				},
				fail: (err) => {
					console.log(err)
					uni.showToast({
						title: '获取失败',
						icon: "none"
					})
					reject(err)
				}
			})
		})
	}
}